/**
 * @autor Flower
 * @descrption 亲友列表
 */

import HttpService from '../../../../services/HttpService'
import ApiUrl from '../../../../services/ApiUrl'
import EditType from '../edit-types'
import Util from '../../../../services/Util'

Page({
  data: {
     memberList: wx.getStorageSync('members') || []
  },
  getMemberlist: function(){
    let self = this;
    let opts = {
      apiObj:ApiUrl.members
    }
    HttpService.accessAPI(opts).then(function(result){
      console.log( 'get member list success', result );
      for( let i =0;i<result.data.length;i++ ){
        if( !result.data[i].avatar ) result.data[i].avatar = '/images/avatar-placeholder.jpeg';
        if( result.data[i].birthday ) result.data[i].birthday = Util.unixToDate(result.data[i].birthday);
      }
      console.log( 'testteteststst',result.data )
      self.setData({
        memberList: result.data
      })
      wx.setStorage({key:'members', data: result.data || []});
    });
  },
  goEditMember: function(e){
     console.log( 'e', e.target.dataset.member );
     let member = e.target.dataset.member;
     if( !member ) return;
     wx.navigateTo({ url:"/pages/user/member/edit-member/edit-member?type="+EditType.member.editInfo+'&name='+member.name+'&birthday='+member.birthday+'&id='+member.id+'&avatar='+member.avatar });
  },
  //更新页面数据
  updatePageData: function(){
    let self = this;
    self.setData({
        memberList: wx.getStorageSync('members') || []
    });
  },
  onLoad: function (option) {
    console.log('list member onLoad')
    let self = this;
    self.getMemberlist();
  },
  onShow: function(){
    let self = this;
    self.updatePageData();
  }
})